// flyer/flyerCardAndCoupon/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeofCard: 0,
    flyerid: 0,
    couponTypeList: [],
    flyerCardItemList: []
  },

  changeType: function (e) {
    let typeofCard = e.currentTarget.dataset.typeofcard;
    this.setData({
      typeofCard: typeofCard
    })
  },
  addProjectFlyer: function () {
    wx.navigateTo({
      url: '/merchant/share/addProjectShare/index?flyerid=' + this.data.flyerid,
    })
  },

  // 修改卡项目
  updateCardItem: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/merchant/share/addProjectShare/index?cid=' + id + '&flyerid=' + this.data.flyerid,
    })
  },
  // 删除卡项目
  deleteCardItem: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/DeleteCardItem?id=' + id + '&uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 2000
                })
                that.getGradePackageInfo(that.data.flyerid);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  // 关闭优惠券
  updateCouponState: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let state = e.currentTarget.dataset.state;
    wx.showModal({
      title: '提示',
      content: '作废后不可恢复，是否确认？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/ChangeCouponTypeState?couponTypeId=' + coupontypeid + '&state=' + state,
            success: function (res) {
              if (res.data.Code == 0) {
                if (!state) {
                  wx.showToast({
                    title: '优惠券已关闭',
                    duration: 2000
                  })
                }
                that.getGradePackageInfo(that.data.flyerid);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  // 删除优惠券
  deleteCoupon: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    wx.showModal({
      title: '提示',
      content: '删除后，已发放的该张优惠券失效，是否操作？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/DeleteCouponType?couponTypeId=' + coupontypeid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 1000
                })
                that.getGradePackageInfo(that.data.gradeid);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  toDetail: function (e) {
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/merchant/merchants/addCoupon/index?coupontypeid=' + coupontypeid + '&type=' + type + '&flyerid=' + this.data.flyerid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.flyerid) {
      this.setData({
        flyerid: options.flyerid
      })
    }
    if (options.typeofCard) {
      this.setData({
        typeofCard: options.typeofCard
      })
    }
  },

  // 传单礼包
  getGradePackageInfo: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Leaflet/GetGradePackageInfo?gradeId=' + that.data.flyerid,
      success: function (res) {
        if (res.data.Code == 0) {
          let couponTypeList = res.data.Data.couponTypeList;
          if (couponTypeList.length > 0) {
            let date = new Date();
            for (let i = 0; i < couponTypeList.length; i++) {
              var date2 = new Date(Date.parse(couponTypeList[i].sendendtime.replace(/-/g, "/")));
              if (date2 < date) {
                couponTypeList[i].isExpiretime = 1;
              } else {
                couponTypeList[i].isExpiretime = 0;
              }
              couponTypeList[i].sendstarttime = couponTypeList[i].sendstarttime.substr(0, 10);
              couponTypeList[i].sendendtime = couponTypeList[i].sendendtime.substr(0, 10);
            }
          }
          that.setData({
            flyerCardItemList: res.data.Data.cardItemList,
            couponTypeList: couponTypeList
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGradePackageInfo()
  }
})